package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.secureplat.common.core.constant.CacheConstants;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.entity.BasicCustomizeTableEntity;
import com.iwomy.secureplat.platform.mapper.BasicCustomizeTableMapper;
import com.iwomy.secureplat.platform.service.BasicCustomizeTableService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
 * 自定义表格
 *
 * @author pig
 * @date 2024-01-12 11:11:31
 */
@Service
public class BasicCustomizeTableServiceImpl extends ServiceImpl<BasicCustomizeTableMapper, BasicCustomizeTableEntity> implements BasicCustomizeTableService {
	@Override
	@Cacheable(value = CacheConstants.CUSTOMIZE_TABLE, key = "#basicCustomizeTable.userId + ' ' + #basicCustomizeTable.tableName")
	public R getBasicCustomizeTable(BasicCustomizeTableEntity basicCustomizeTable) {
		BasicCustomizeTableEntity customizeTable = this.getOne(Wrappers.lambdaQuery(BasicCustomizeTableEntity.class)
				.eq(BasicCustomizeTableEntity::getTableName, basicCustomizeTable.getTableName())
				.eq(BasicCustomizeTableEntity::getUserId, SecurityUtils.getUser().getId())
				.orderByDesc(BasicCustomizeTableEntity::getCreateTime).last("limit 1"));

		if (ObjectUtil.isNotNull(customizeTable)) return R.ok(customizeTable);

		return R.ok(null);
	}
}